********************************************************************
* LOV3
* Niklas Jakobsson
********************************************************************
* Program setup 
clear all						// Start with a clean slate
macro drop _all         		// Clear all macros
set more off            		// Disable partitioned output
version 18  					// Set Version number

********************************************************************
* Directory settings (create the folder "MalmoLOV" and change the file path to the project folder, within that folder create the folders "data", "figures", "tables", "dofiles", "logfiles")
global proj_name "MalmoLOV"								// Name of the folder of the project
global proj_main "C:/Users/nikljako/Box/${proj_name}"	// File path to the folder where the project folder is (change this to your file path)
global datadir "${proj_main}/data"
global figdir "${proj_main}/figures"
global tabledir "${proj_main}/tables"
global dodir "${proj_main}/do-files"
global figdir "${proj_main}/figures" 
global logdir "${proj_main}/logfiles"

********************************************************************
* Open analysis data (prepared in 1_build_data.do)
use $datadir/LOV3_data_for_analysis.dta, clear

************************************************
*********** Results for Mollevangen	 ***********
************************************************
preserve
keep if id == 32
gen numeric_date = date
* Keep three months before and after
drop if date < td(2nov2020)
drop if date > td(14sep2021)

* Malmö Möllevången
generate treated = 1 if id == 32 & date >td(1feb2021) //Decision 2feb2021
replace treated = 0 if id == 32 & treated == .
generate treated1 = 1 if id == 32 & date >td(13jun2021) //Twelve guards mon-sun 14jun2021
replace treated1 = 0 if id == 32 & treated1 == .

* Create the two interaction variables for the analysis
bysort id: gen time = _n
xtset id time
bys id (time): gen temp = time if treated==1 & treated[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact = wanted + 1
replace interact = 0 if interact < 0
drop temp temp2 wanted

xtset id time
bys id (time): gen temp = time if treated1==1 & treated1[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact1 = wanted + 1
replace interact1 = 0 if interact1 < 0
drop temp temp2 wanted

tsset date

*reg crime_negative time treated interact treated1 interact1, robust	//With different slopes
reg crime_negative time treated treated1, robust						//With same slopes
outreg2 using $tabledir/table1, word replace bdec(3)					//Save regression output in table1.rtf

predict yhat
predict se_yhat, stdp
gen ci_upper = yhat + 1.96 * se_yhat
gen ci_lower = yhat - 1.96 * se_yhat

set scheme s1mono
twoway (rarea ci_upper ci_lower date if date <td(2feb2021), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper ci_lower date if date >td(2feb2021) & date <td(14jun2021), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper ci_lower date if date >td(14jun2021), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat date if date <td(2feb2021), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(2feb2021) & date <td(14jun2021), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(14jun2021), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22221 22313 22445 22537) xlabel(,labsize(vsmall)) title("Mollevangen") ytitle("Non police initiated crimes") xtitle("") yscale(r(-0.5 2)) ylabel(-0.5(0.5)2)  xlabel(, ang(vertical)) saving($figdir/molle, replace)

*reg crime_positive time treated interact treated1 interact1, robust
reg crime_positive time treated treated1, robust
outreg2 using $tabledir/table1, word append bdec(3)

predict yhat2
predict se_yhat2, stdp
gen ci_upper2 = yhat2 + 1.96 * se_yhat2
gen ci_lower2 = yhat2 - 1.96 * se_yhat2

set scheme s1mono
twoway (rarea ci_upper2 ci_lower2 date if date <td(2feb2021), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper2 ci_lower2 date if date >td(2feb2021) & date <td(14jun2021), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper2 ci_lower2 date if date >td(14jun2021), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat2 date if date <td(2feb2021), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(2feb2021) & date <td(14jun2021), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(14jun2021), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22221 22313 22445 22537) xlabel(,labsize(vsmall)) ytitle("Police initiated crimes") xtitle("Date", color(white)) yscale(r(-0.5 2)) ylabel(-0.5(0.5)2)  xlabel(, ang(vertical)) saving($figdir/molle_pos, replace)

restore	   
	   
************************************************
******* Results for Central Helsingborg  *******
************************************************
preserve

keep if id == 6
gen numeric_date = date
* Keep three months before and after
drop if date < td(26oct2021)
drop if date > td(1oct2022)
tsset date

* Helsngborg centrum
generate treated = 1 if id == 6 & date >td(25jan2022)	//Decision 26jan2022
replace treated = 0 if id == 6 & treated == . 
generate treated1 = 1 if id == 6 & date >td(8feb2022)	//Two guards wed-sat 9feb2022
replace treated1 = 0 if id == 6 & treated1 == . 					
generate treated2 = 1 if id == 6 & date >td(30jun2022)	//Two guards mon-sat 1jul2022
replace treated2 = 0 if id == 6 & treated2 == .

* Generate the three interaction variables for the analysis
bysort id: gen time = _n
xtset id time
bys id (time): gen temp = time if treated==1 & treated[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact = wanted + 1
replace interact = 0 if interact < 0
drop temp temp2 wanted

xtset id time
bys id (time): gen temp = time if treated1==1 & treated1[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact1 = wanted + 1
replace interact1 = 0 if interact1 < 0
drop temp temp2 wanted 

xtset id time
bys id (time): gen temp = time if treated2==1 & treated2[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact2 = wanted + 1
replace interact2 = 0 if interact2 < 0
drop temp temp2 wanted 

*reg crime_negative time treated interact treated1 interact1 treated2 interact2, robust
reg crime_negative time treated treated1 treated2, robust
outreg2 using $tabledir/table1, word append bdec(3)

predict yhat, xb
predict se_yhat, stdp
gen ci_upper = yhat + 1.96 * se_yhat
gen ci_lower = yhat - 1.96 * se_yhat

set scheme s1mono
twoway (rarea ci_upper ci_lower date if date <td(26jan2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper ci_lower date if date >td(26jan2022) & date <td(9feb2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper ci_lower date if date >td(9feb2022) & date <td(1jul2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper ci_lower date if date >td(1jul2022), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat date if date <td(26jan2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(26jan2022) & date <td(9feb2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(9feb2022) & date <td(1jul2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(1jul2022), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22579 22671 22685 22827 22919) xlabel(,labsize(vsmall)) title("Central Helsingborg")ytitle("") xtitle("") yscale(r(-0.5 2)) ylabel(-0.5(0.5)2) xlabel(, ang(vertical)) saving($figdir/helsingc, replace)

*reg crime_positive time treated interact treated1 interact1 treated2 interact2, robust
reg crime_positive time treated treated1 treated2, robust
outreg2 using $tabledir/table1, word append bdec(3)

predict yhat2, xb
predict se_yhat2, stdp
gen ci_upper2 = yhat2 + 1.96 * se_yhat2
gen ci_lower2 = yhat2 - 1.96 * se_yhat2

set scheme s1mono
twoway (rarea ci_upper2 ci_lower2 date if date <td(26jan2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper2 ci_lower2 date if date >td(26jan2022) & date <td(9feb2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper2 ci_lower2 date if date >td(9feb2022) & date <td(1jul2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper2 ci_lower2 date if date >td(1jul2022), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat2 date if date <td(26jan2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(26jan2022) & date <td(9feb2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(9feb2022) & date <td(1jul2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(1jul2022), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22579 22671 22685 22827 22919) xlabel(,labsize(vsmall)) ytitle("") xtitle("Date") yscale(r(-0.5 2)) ylabel(-0.5(0.5)2) xlabel(, ang(vertical)) saving($figdir/helsingc_pos, replace)

restore

************************************************
******* Results for Southern Helsingborg *******
************************************************
preserve

keep if id == 4
gen numeric_date = date
tsset date
* Drop some data before and after, decide on the exact window later
drop if date < td(26mar2020)
drop if date > td(1nov2022)
*itsa crime_negative, single trperiod(9sep2020; 1aug2022) fig

* Helsingborg söder
generate treated = 1 if id == 4 & date >td(25jun2020)	//Decision 26jun2020
replace treated = 0 if id == 4 & treated == .
generate treated1 = 1 if id == 4 & date >td(9sep2020)	//Two guards tor-fre 10sep2020
replace treated1 = 0 if id == 4 & treated1 == .
generate treated2 = 1 if id == 4 & date >td(31dec2021)	//Two guards wed-sat 1jan2022
replace treated2 = 0 if id == 4 & treated2 == .
generate treated3 = 1 if id == 4 & date >td(31jul2022)	//Two guards mon-sat 1aug2022
replace treated3 = 0 if id == 4 & treated3 == .		

* Generate the four interaction variables for the analysis
bysort id: gen time = _n
xtset id time
bys id (time): gen temp = time if treated==1 & treated[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact = wanted + 1
replace interact = 0 if interact < 0
drop temp temp2 wanted

xtset id time
bys id (time): gen temp = time if treated1==1 & treated1[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact1 = wanted + 1
replace interact1 = 0 if interact1 < 0
drop temp temp2 wanted 

xtset id time
bys id (time): gen temp = time if treated2==1 & treated2[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact2 = wanted + 1
replace interact2 = 0 if interact2 < 0
drop temp temp2 wanted 

xtset id time
bys id (time): gen temp = time if treated3==1 & treated3[_n-1]==0
bys id (time): egen temp2 = total(temp)
gen wanted = time-temp2
bys id (time): replace wanted = [_n-1]-_N if temp2==0
bys id (time): gen interact3 = wanted + 1
replace interact3 = 0 if interact3 < 0
drop temp temp2 wanted 

*reg crime_negative time treated interact treated1 interact1 treated2 interact2 treated3 interact3, robust
reg crime_negative time treated treated1 treated2 treated3, robust
outreg2 using $tabledir/table1, word append bdec(3)

predict yhat, xb
predict se_yhat, stdp
gen ci_upper = yhat + 1.96 * se_yhat
gen ci_lower = yhat - 1.96 * se_yhat
   
set scheme s1mono
twoway (rarea ci_upper ci_lower date if date <td(26jun2020), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper ci_lower date if date >td(26jun2020) & date <td(10sep2020), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper ci_lower date if date >td(10sep2020) & date <td(1jan2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper ci_lower date if date >td(1jan2022) & date <td(1aug2022), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper ci_lower date if date >td(1aug2022), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat date if date <td(26jun2020), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(26jun2020) & date <td(10sep2020), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(10sep2020) & date <td(1jan2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(1jan2022) & date <td(1aug2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat date if date >td(1aug2022), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22000 22092 22168 22646 22858 22950) xlabel(,labsize(vsmall)) title("Southern Helsingborg") ytitle("") xtitle("") yscale(r(-0.5 2)) ylabel(-0.5(0.5)2) xlabel(, ang(vertical)) saving($figdir/helsings, replace)  
   
*reg crime_positive time treated interact treated1 interact1 treated2 interact2 treated3 interact3, robust
reg crime_positive time treated treated1 treated2 treated3, robust
outreg2 using $tabledir/table1, word append bdec(3)

predict yhat2, xb
predict se_yhat2, stdp
gen ci_upper2 = yhat2 + 1.96 * se_yhat2
gen ci_lower2 = yhat2 - 1.96 * se_yhat2
   
set scheme s1mono
twoway (rarea ci_upper2 ci_lower2 date if date <td(26jun2020), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper2 ci_lower2 date if date >td(26jun2020) & date <td(10sep2020), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper2 ci_lower2 date if date >td(10sep2020) & date <td(1jan2022), sort fcolor(gs12) lwidth(none)) || ///
	   (rarea ci_upper2 ci_lower2 date if date >td(1jan2022) & date <td(1aug2022), sort fcolor(gs12) lwidth(none)) || ///
       (rarea ci_upper2 ci_lower2 date if date >td(1aug2022), sort fcolor(gs12) lwidth(none)) || ///
	   (line yhat2 date if date <td(26jun2020), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(26jun2020) & date <td(10sep2020), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(10sep2020) & date <td(1jan2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(1jan2022) & date <td(1aug2022), sort lpattern(dash) lcolor(black)) || ///
	   (line yhat2 date if date >td(1aug2022), sort lpattern(dash) lcolor(black)) || ///
	   , leg(off) xlabel(22000 22092 22168 22646 22858 22950) xlabel(,labsize(vsmall)) ytitle("") xtitle("Date", color(white)) yscale(r(-0.5 2)) ylabel(-0.5(0.5)2) xlabel(, ang(vertical)) saving($figdir/helsings_pos, replace)  

restore

* Make Figure 1 in the paper
graph combine $figdir/molle.gph $figdir/helsingc.gph $figdir/helsings.gph $figdir/molle_pos.gph $figdir/helsingc_pos.gph 	
graph export $figdir/mygraph.pdf, replace //Save the figure



